import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store'

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// 白名单列表，用路由的name设置
export const WHITE_LIST_NAMES = ['404', 'login', 'dashboard', 'account']

export const constantRoutes = [
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue'),
    hidden: true
  },
  {
    path: '/out-link',
    component: () => import('@/views/out-link'),
    hidden: true
  },
  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/device'
  // }
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { menuId: 1, icon: 'menu-dashboard' },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', menuId: 'M_0' }
      }
    ]
  },
  {
    path: '/system/ram/detail/:userId',
    component: Layout,
    children: [
      {
        path: '',
        name: 'userDetail',
        component: () => import('@/views/system/ram/detail.vue')
      }
    ]
  }
]

export const asyncRoutes = [
  {
    path: '/system-menu/level/manage',
    component: Layout,
    children: [
      {
        name: 'levelManage',
        path: '',
        component: () => import('@/views/system-menu/level/rank-manage')
      }
    ]
  },
  {
    path: '/tms/terminal/blacklist',
    component: Layout,
    children: [
      {
        name: 'blacklist',
        path: '',
        component: () => import('@/views/tms/terminal/blacklist')
      }
    ]
  },
  {
    path: '/tms/resources/app-detail',
    component: Layout,
    children: [
      {
        name: 'appDetail',
        path: '',
        component: () => import('@/views/tms/resources/app-detail'),
        meta: { noPadding: true }
      }
    ]
  },
  {
    path: '/tms/resources/app-version-detail',
    component: Layout,
    children: [
      {
        name: 'appVersionDetail',
        path: '',
        component: () => import('@/views/tms/resources/app-version-detail'),
        meta: { noPadding: true }
      }
    ]
  },
  {
    path: '/task/index',
    component: Layout,
    children: [
      {
        name: 'task',
        path: '',
        component: () => import('@/views/task/index.vue')
      }
    ]
  },
  {
    path: '/tms/terminal/view',
    component: Layout,
    children: [
      {
        name: 'deviceView',
        path: '',
        component: () => import('@/views/tms/terminal/device-view.vue')
      }
    ]
  },
  {
    path: '/tms/group/index',
    component: Layout,
    children: [
      {
        name: 'batchIndex',
        path: '',
        component: () => import('@/views/tms/group/index'),
        children: [
          {
            path: ':id',
            component: () => import('@/views/tms/group/batch'),
            meta: {}
          }
        ]
      }
    ]
  },
  {
    path: '/operator/operator-detail/:operatorId',
    component: Layout,
    children: [
      {
        name: 'operator-Info',
        path: '',
        component: () => import('@/views/operator/operator-detail.vue'),
        meta: { noPadding: true }
      }
    ]
  },
  {
    path: '/operator/audit-detail/:operatorId',
    component: Layout,
    children: [
      {
        name: 'audit-Info',
        path: '',
        component: () => import('@/views/operator/audit-detail.vue')
      }
    ]
  },
  {
    path: '/developer/developer-detail/:developerId',
    component: Layout,
    children: [
      {
        name: 'developerInfo',
        path: '',
        component: () => import('@/views/developer/developer-detail.vue'),
        meta: { noPadding: true }
      }
    ]
  },
  {
    path: '/platform-report/operator-detail',
    component: Layout,
    children: [
      {
        name: 'operatorInfo',
        path: '',
        component: () => import('@/views/platform-report/operator-detail.vue')
      }
    ]
  },
  {
    path: '/platform-report/flyiot-report-detail',
    component: Layout,
    children: [
      {
        name: 'flycareReportDetail',
        path: '',
        component: () =>
          import('@/views/platform-report/component/flyiot-report-detail')
      }
    ]
  },
  {
    path: '/appstore/test/test-detail',
    component: Layout,
    children: [
      {
        name: 'testDetail',
        path: '',
        component: () => import('@/views/appstore/test-detail.vue')
      }
    ]
  },
  {
    path: '/appstore/list/dev-app-detail',
    component: Layout,
    children: [
      {
        name: 'developer-app-detail',
        path: '',
        component: () => import('@/views/appstore/components/app-detail.vue')
      }
    ]
  },
  {
    path: '/service-management/contract/add',
    component: Layout,
    children: [
      {
        path: '',
        name: 'addContract',
        component: () =>
          import('@/views/service-management/contract/add-contract.vue')
      }
    ]
  },
  {
    path: '/service-management/contract/edit',
    component: Layout,
    children: [
      {
        path: '',
        name: 'editContract',
        component: () =>
          import('@/views/service-management/contract/edit-contract.vue')
      }
    ]
  },
  {
    path: '/service-management/contract/detail',
    component: Layout,
    children: [
      {
        path: '',
        name: 'detailContract',
        component: () =>
          import('@/views/service-management/contract/detail-contract.vue')
      }
    ]
  },
  {
    path: '/service-management/statement/detail',
    component: Layout,
    children: [
      {
        path: '',
        name: 'detailStatement',
        component: () =>
          import('@/views/service-management/statement/detail-statement.vue')
      }
    ]
  },
  {
    path: '/service-management/customer-report/detail',
    component: Layout,
    children: [
      {
        path: '',
        name: 'detailReport',
        component: () =>
          import('@/views/service-management/customer-report/detail-report.vue')
      }
    ]
  },
  {
    path: '/service-management/oa-bill/detail',
    component: Layout,
    children: [
      {
        path: '',
        name: 'detailInvoice',
        component: () =>
          import('@/views/service-management/oa-bill/detail-invoice.vue')
      }
    ]
  },
  {
    path: '/tms/group/task/:batchId',
    component: Layout,
    children: [
      {
        name: 'addTask',
        path: '',
        component: () => import('@/views/tms/group/components/add-task.vue')
      }
    ]
  },
  {
    path: '/tms/group/task/detail/:batchOperationId',
    component: Layout,
    children: [
      {
        name: 'taskDetail',
        path: '',
        component: () =>
          import('@/views/tms/group/components/batch-operation-detail.vue')
      }
    ]
  },
  {
    path: '/tms/group/task/detail/command/:operationKeyId',
    component: Layout,
    children: [
      {
        name: 'taskDetailCmd',
        path: '',
        component: () =>
          import(
            '@/views/tms/group/components/batch-operation-detail-command.vue'
          )
      }
    ]
  },
  {
    path: '/tms/batch/task/settings/:batchId',
    component: Layout,
    children: [
      {
        name: 'addTaskSettings',
        path: '',
        component: () => import('@/views/tms/group/components/Settings.vue')
      }
    ]
  },
  {
    path: '/tms/group/task/settings/detail',
    component: Layout,
    children: [
      {
        name: 'settingsDetail',
        path: '',
        component: () =>
          import('@/views/tms/group/components/SettingsDetail.vue')
      }
    ]
  },
  {
    path: '/fly-parameter/config/:id',
    component: Layout,
    children: [
      {
        name: 'FlyParameterConfig',
        path: '',
        component: () => import('@/views/fly-parameter/config.vue'),
        meta: { noPadding: true }
      }
    ]
  }
]

export const childrenRoutes = [
  // {
  //   path: '/contract/index',
  //   name: 'contract',
  //   component: () => import('@/views/service-management/contract/index'),
  //   hidden: true,
  //   parentId: 'M_10401000',
  //   meta: {}
  // },
  // {
  //   path: '/statement/index',
  //   name: 'statement',
  //   component: () => import('@/views/service-management/statement/index'),
  //   hidden: true,
  //   parentId: 'M_10401002',
  //   meta: {}
  // },
  // {
  //   path: '/report/index',
  //   name: 'report',
  //   component: () => import('@/views/service-management/customer-report/index'),
  //   hidden: true,
  //   parentId: 'M_10401004',
  //   meta: {}
  // },
  // {
  //   path: '/oa-bill/index',
  //   name: 'oa-bill',
  //   component: () => import('@/views/service-management/oa-bill/index'),
  //   hidden: true,
  //   parentId: 'M_10401006',
  //   meta: {}
  // }
  // {
  //   path: ':id',
  //   component: () => import('@/views/batch/batch'),
  //   hidden: true,
  //   parentId: 'M_104022',
  //   meta: {}
  // }
]

const router = createRouter({
  // 创建一个 hash 历史记录。
  routes: constantRoutes,
  history: createWebHashHistory(),
  scrollBehavior: () => ({ left: 0, top: 0 })
})

// console.log(router)

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !WHITE_LIST_NAMES.includes(name)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
  store.dispatch('permission/resetRoutes')
}

export function setupRouter(app) {
  app.use(router)
}

export default router
